package cn.edu.scau.cmi.hekaimin.Hibernate.controller;

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;

import java.util.List;

import cn.edu.scau.cmi.hekaimin.Hibernate.dao.StudentDao;
import cn.edu.scau.cmi.hekaimin.Hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.hekaimin.Hibernate.domain.Student;
import cn.edu.scau.cmi.hekaimin.Hibernate.domain.Teacher;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;

public class RootFXController {
	@FXML
	private TableView S_table;
	@FXML
	private Button Sbtn_1;
	@FXML
	private Button Sbtn_2;
	@FXML
	private TableView T_table;
	@FXML
	private Button T_btn_1;
	@FXML
	private Button T_btn_2;
	@FXML
	private TableColumn sid;
	@FXML
	private TableColumn sname;
	@FXML
	private TableColumn stea;
	@FXML
	private TableColumn tid;
	@FXML
	private TableColumn tname;
	@FXML
	private TableColumn tstu;
	@FXML
	private TextField newID;
	@FXML
	private TextField newName;
	
	@FXML
	private TextField tnewID;
	@FXML
	private TextField tnewName;
    // 读取数据库信息更新学生与教师列表
	private ObservableList<Student> students = FXCollections.observableArrayList();
	private ObservableList<Teacher> teachers = FXCollections.observableArrayList();
	public void init() {
		StudentDao stu = new StudentDao();
		TeacherDao tea = new TeacherDao();
		List<Student> students2 = stu.getAll();	 	
		List<Teacher> teachers2 = tea.getAll();
		
		for(Teacher e: teachers2) {
			
			teachers.add(e);
		}
		for(Student e: students2) {
			students.add(e);
		}
		
		sid.setCellValueFactory(new PropertyValueFactory("s_id"));
		sname.setCellValueFactory(new PropertyValueFactory("s_name"));
		stea.setCellValueFactory(new PropertyValueFactory("teacher"));
		
		sname.setCellFactory(TextFieldTableCell.forTableColumn());
		sname.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Student, String>>() {
      		@Override
			public void handle(CellEditEvent<Student, String> t) {
				  Student x = ((Student)t.getTableView().getItems().get(t.getTablePosition().getRow()));    
				  int flag = 0,j;
				  x.setS_name(t.getNewValue());
				  
				  int i;
				  for(i=0;i<students.size();i++) {
					  if( students.get(i).getS_id() == x.getS_id()) {
						  students.get(i).setS_name(t.getNewValue());
						  break;
					  }
				  }
				 
				  
				  StudentDao dao = new StudentDao();
				  dao.update(x);
				  List<Teacher> teachers2 = tea.getAll();
				  teachers.clear();
				  for(Teacher e: teachers2) {
						
						teachers.add(e);
					}
				  
			}
		});
		
		
		tid.setCellValueFactory(new PropertyValueFactory("t_id"));
		tname.setCellValueFactory(new PropertyValueFactory("t_name"));
		tstu.setCellValueFactory(new PropertyValueFactory("students"));
		
		tname.setCellFactory(TextFieldTableCell.forTableColumn());
		tname.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Teacher, String>>() {
      		@Override
			public void handle(CellEditEvent<Teacher, String> t) {
				  Teacher x = ((Teacher)t.getTableView().getItems().get(t.getTablePosition().getRow()));        
				  x.setT_name(t.getNewValue());
				  int i;
				  for(i=0;i<teachers.size();i++) {
					  if( teachers.get(i).getT_id() == x.getT_id()) {
						  teachers.get(i).setT_name(t.getNewValue());
						  break;
					  }
				  }
				  TeacherDao dao = new TeacherDao();
				  dao.update(x);
				  List<Student> students2 = stu.getAll();	
				  students.clear();
				  for(Student e: students2) {
						students.add(e);
					}	  
			}
		});
		S_table.setItems(students);
		T_table.setItems(teachers);		
	}
	@FXML
	public void addStu(ActionEvent event) {
		if(newName.getText().equals("")) {
			return;
		}else {
			Student stu = new Student();
			//stu.setS_id(Integer.parseInt(newID.getText()));
			stu.setS_name(newName.getText());
			students.add(stu);
			StudentDao dao = new StudentDao();
			dao.add(stu);
		}
		newName.setText("");
	}

	@FXML
	public void deleStu(ActionEvent event) {
		if(newID.getText().equals("")) {
			return;
		}else {
			
			StudentDao stu = new StudentDao();
			List<Student> students2 = stu.getAll();	 
			for(int i = 0; i <students2.size();i++) {
				if(students2.get(i).getS_id() == Integer.parseInt(newID.getText())) {
					if(students2.get(i).getTeacher() == null)
						break;
					students2.get(i).setTeacher(null);
					Student x = students2.get(i);
					stu.update(x);                //更新数据库信息 除去student与导师的关联
					break;
				}
			}
			stu.delete(Integer.parseInt(newID.getText()));
			
			TeacherDao tea = new TeacherDao();
			List<Teacher> teachers2 = tea.getAll();
			teachers.clear();
			for(Teacher e:teachers2) {
				teachers.add(e);
			}
			int i;
			for(i=0;i<students.size();i++) {
				if(students.get(i).getS_id() == Integer.parseInt(newID.getText())) {
					break;
				}
			}
			students.remove(i);
			
		}
		newID.setText("");
	}
	@FXML
	public void addTea(ActionEvent event) {
		if(tnewName.getText().equals("")) {
			return;
		}else {
			Teacher tea = new Teacher();
			tea.setT_name(tnewName.getText());
			teachers.add(tea);
			TeacherDao dao = new TeacherDao();
			dao.add(tea);
		}
		tnewName.setText("");
	}
	@FXML
	public void deleTea(ActionEvent event) {
		if(tnewID.getText().equals("")) {
			return;
		}else {			
			StudentDao stu = new StudentDao();
			List<Student> students2 = stu.getAll();
			for(int i = 0; i < students2.size();i++) {   // 更新student 解除与teacher的关系,便于删除.
				Teacher t = students2.get(i).getTeacher();
				if(t !=null &&t.getT_id() == Integer.parseInt(tnewID.getText()) ){   //判断是否为 student含有与老师的关联
					students2.get(i).setTeacher(null);
					stu.update(students2.get(i));
				    students.clear();           //更新student table
			        for(Student e: students2) {
				       students.add(e);
			        }
					break;
				}
			}
			TeacherDao tea = new TeacherDao();
			tea.delete(Integer.parseInt(tnewID.getText()));
			int i;
			for(i=0;i<teachers.size();i++) {
				if(teachers.get(i).getT_id() == Integer.parseInt(tnewID.getText())) {
					break;
				}
			}
			teachers.remove(i);	
		}
		tnewID.setText("");
	}
}
